<?php
/**
 * Created by PhpStorm.
 * User: v_jingzhili
 * Date: 2019/4/2
 * Time: 16:11
 */


namespace Ctrl;

use Lib;
use Lib\Paas\Role;
use Lib\Base\Page;

class Operation extends Lib\Base\Ctrl
{

    private $db;

    public function __construct()
    {
        //校验管理员权限
        $roleService = new Role();
        $checkAdmin = $roleService->getRoleInfo();
//        if ($checkAdmin['role'] != ROLE_ID) {
//            die('抱歉！您没有访问权限');
//        }
//        if (!in_array(MODULE_ID, $checkAdmin['moduleID']) ) {
//            die('抱歉！您没有访问权限');
//        }
        $this->db = Lib\Db\DbModel::init();
    }

    public function index()
    {
        $p = I('get.page', 1, 'intval');
        $page_num_set = I('limit');//单页数据量
        if ($page_num_set) {
            $page_num_set = $page_num_set;
        } else {
            $page_num_set = Page::LISTROW20;
        }
        $where = '';

        //关键词搜索
        $search = trim(I('get.search', ''));
        if ($search) {
            $where .= "name like '%{$search}%'";
            $this->assign('search', $search);
        }
        $type = trim(I('get.type', ''));
        if ($type) {
            if (!empty($where)) {
                $where .= " and ";
            }
            $where .= "type = " . $type;
            $this->assign('type', $type);
        }
        //时间范围搜索
        $date = trim(I('get.date', ''));
        if ($date) {
            $this->assign('date', $date);
            $time = explode(' - ', $date);
            if (count($time) === 2) {
                $startTime = $time[0];
                $endTime = $time[1];
                if (!empty($where)) {
                    $where .= " and ";
                }
                $where .= "createTime between '{$startTime}' and '{$endTime}'";
            }
        }

        if (empty($where)) {
            $where = 1;
        }
        $sql = "SELECT COUNT(*) as count FROM tblog WHERE " . $where;
        $count = $this->db->query($sql, 1)['count'];

        $page = new Page($count, $page_num_set);
        $show = $page->show();
        $this->assign('page', $show);
        $start = ($p - 1) * $page_num_set;
        $sql = "SELECT * FROM tblog WHERE {$where} ORDER BY id DESC LIMIT {$start},{$page_num_set}";
        $log = $this->db->query($sql);
        $this->assign('log', $log);
        $this->assign('count', $count);
        $this->display('Admin/Operation/index');
    }


    /**
     * 数据清理
     */
    public function clean()
    {
        //默认清理前三个月的数据
        $mtime= date("Y-m-d H:i:s", strtotime("-3 month"));

        $versionIds = $this->db->table('tbversion')->where(['createTime' =>['<',$mtime]])->select('id');

        if(empty($versionIds)){
            $this->ajaxReturn([
                'type' => 0,
                'status' => 'error',
                'msg' => "没有需要清理的数据",
            ], 'json');
        }

        //删除对应版本的数据
        try {
            $this->db->begin();

            foreach ($versionIds as $vid){

                //删除英雄相关数据
                $this->db->table('tbhero')->where(['versionId' => $vid])->delete();
                $this->db->table('tbspells')->where(['versionId' => $vid])->delete();

                //删除道具数据
                $this->db->table('tbitems')->where(['versionId' => $vid])->delete();

                //删除皮肤数据
                $this->db->table('tbskins')->where(['versionId' => $vid])->delete();

                //删除版本数据
                $this->db->table('tbversion')->where(['id' => $vid])->delete();

                logWrite(parent::LOG_TYPE_DEL,true,'tbversion',"数据清理成功版本$vid");
            }
            $this->db->commit();

            $this->ajaxReturn([
                'type' => 1,
                'status' => 'success',
                'msg' => "清理成功",
            ], 'json');

        } catch (\Exception $e) {
            $this->db->rollBack();

            logWrite(parent::LOG_TYPE_DEL,false,'tbversion','数据清理失败');
            $this->ajaxReturn([
                'type' => 0,
                'status' => 'error',
                'msg' => "清理失败",
            ], 'json');
        }
    }

}